package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.entity.Label;
import com.smart.mapper.LabelMapper;
import com.smart.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    LabelMapper labelMapper;

    @Override
    public Map<String, Object> ArticleLabelAll(Integer currentPage, Integer pageSize) {
        Page<Label> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        QueryWrapper<Label> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("label_id","label_name");
        IPage<Label> iPage = labelMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Integer ArticleLabelAdd(String labelname) {
        QueryWrapper<Label> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("label_name",labelname);
        Long aLong = labelMapper.selectCount(queryWrapper);
        if (aLong > 0) {
            return 0;
        }
        Label label = new Label();
        label.setLabelName(labelname);
        int result = labelMapper.insert(label);
        return result;
    }


    @Override
    public Integer ArticleLabelDetle(Integer id) {
        Label label = new Label();
        label.setLabelId(id);
        int result = labelMapper.deleteById(label);
        return result;
    }

    @Override
    public Label ArticleByLabel(Integer id) {
        Label label = labelMapper.selectById(id);
        return label;
    }

    @Override
    public Integer ArticleLabelEdit(Label label) {
        QueryWrapper<Label> labelQueryWrapper = new QueryWrapper<>();
        labelQueryWrapper.eq("label_id",label.getLabelId());
        Integer i = labelMapper.update(label, labelQueryWrapper);
        return i;
    }
}
